#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    this->ui->setupUi(this);
    this->m_sysControl = new sys_Control(this->ui, this);
    this->initUI();
    this->loadQss();
    connect(m_sysControl, sys_Control::fileOpened, this, [&](const QString fileName) {
        this->setWindowTitle("面向CT影像序列的新冠肺炎辅助诊断系统: " + fileName);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    //只允许输入数字
    ui->lineEdit_cursorPositionX->setValidator(new QIntValidator(0, 999, ui->lineEdit_cursorPositionX));
    ui->lineEdit_cursorPositionY->setValidator(new QIntValidator(0, 999, ui->lineEdit_cursorPositionY));
    ui->lineEdit_cursorPositionZ->setValidator(new QIntValidator(0, 999, ui->lineEdit_cursorPositionZ));
    ui->lineEdit_window->setValidator(new QDoubleValidator(ui->lineEdit_window));
    ui->lineEdit_level->setValidator(new QDoubleValidator(ui->lineEdit_level));
    //初始化历史文件列表
    QStringList filePathList = sys_Data::getInstance()->getOpenFileHistory();
    for(const auto& filePath  : filePathList) {
        QAction *action = new QAction(filePath, ui->menu_file_recentFile);
        //toolbutton菜单被点击后触发的事件
        connect(action, &QAction::triggered, this, [&]() {
            dlg_DiskFile::isExist(qobject_cast<QAction*>(sender())->text());
            this->m_sysControl->setImgFile(qobject_cast<QAction*>(sender())->text());
        });
        ui->menu_file_recentFile->addAction(action);
    }
    ui->tbtn_recentFile->setMenu(ui->menu_file_recentFile);
    ui->tbtn_recentFile->setPopupMode(QToolButton::InstantPopup);
    //--------------------------------------------------------------------------------
    ui->comboBox_windowLevel->clear();
    for(int i = 0; i < img_Info::IMG_WL.size(); i++) {
        ui->comboBox_windowLevel->addItem(img_Info::IMG_WL[i].name);
    }
    ui->comboBox_windowLevel->setCurrentIndex(-1);
    //--------------------------------------------------------------------------------
    m_sysControl->setDangerUIClose();
}

void MainWindow::loadQss()
{
    this->setStyleSheet(sys_Data::getInstance()->getThemeQss());
}

void MainWindow::on_action_file_openFile_triggered()
{
    m_sysControl->openDiskFile();
}

void MainWindow::on_action_help_document_triggered()
{
}

void MainWindow::on_action_help_about_triggered()
{
}

void MainWindow::on_action_file_quit_triggered()
{
    exit(0);
}

void MainWindow::on_action_window_windowed_triggered()
{
    this->showNormal();
}

void MainWindow::on_action_window_fullScreen_triggered()
{
    this->showFullScreen();
}

void MainWindow::on_action_window_theme_ElegantDark_triggered()
{
    sys_Data::getInstance()->setThemePath("ElegantDark");
    this->loadQss();
}

void MainWindow::on_action_window_theme_Ubuntu_triggered()
{
    sys_Data::getInstance()->setThemePath("Ubuntu");
    this->loadQss();
}

void MainWindow::on_action_window_theme_Default_triggered()
{
    sys_Data::getInstance()->setThemePath("Default");
    this->loadQss();
}

void MainWindow::on_action_segmentation_open_triggered()
{
    m_sysControl->openAnalysisFile();
}

void MainWindow::on_action_segmentation_save_triggered()
{
    m_sysControl->saveAnalysis();
}

void MainWindow::on_action_segmentation_analysis_triggered()
{
    m_sysControl->analysisImg();
}


void MainWindow::on_btn_view3D_layout_clicked()
{
    static bool flag = true;
    if(flag) {
        flag = false;
        ui->widget_viewA->hide();
        ui->widget_viewC->hide();
        ui->widget_viewS->hide();
        ui->btn_view3D_layout->setText("田");
    } else {
        flag = true;
        ui->widget_viewA->show();
        ui->widget_viewC->show();
        ui->widget_viewS->show();
        ui->btn_view3D_layout->setIcon(QIcon());
        ui->btn_view3D_layout->setText("3D");
    }
}

void MainWindow::on_btn_view3D_layout_2_clicked()
{
    on_btn_view3D_layout_clicked();
}

void MainWindow::on_btn_viewA_layout_clicked()
{
    static bool flag = true;
    if(flag) {
        flag = false;
        ui->widget_view3D->hide();
        ui->widget_viewC->hide();
        ui->widget_viewS->hide();
        ui->btn_viewA_layout->setText("田");
    } else {
        flag = true;
        ui->widget_view3D->show();
        ui->widget_viewC->show();
        ui->widget_viewS->show();
        ui->btn_viewA_layout->setIcon(QIcon());
        ui->btn_viewA_layout->setText("A");
    }
}

void MainWindow::on_btn_viewS_layout_clicked()
{
    static bool flag = true;
    if(flag) {
        flag = false;
        ui->widget_view3D->hide();
        ui->widget_viewC->hide();
        ui->widget_viewA->hide();
        ui->btn_viewS_layout->setText("田");
    } else {
        flag = true;
        ui->widget_view3D->show();
        ui->widget_viewC->show();
        ui->widget_viewA->show();
        ui->btn_viewS_layout->setIcon(QIcon());
        ui->btn_viewS_layout->setText("S");
    }
}

void MainWindow::on_btn_viewC_layout_clicked()
{
    static bool flag = true;
    if(flag) {
        flag = false;
        ui->widget_view3D->hide();
        ui->widget_viewS->hide();
        ui->widget_viewA->hide();
        ui->btn_viewC_layout->setText("田");
    } else {
        flag = true;
        ui->widget_view3D->show();
        ui->widget_viewS->show();
        ui->widget_viewA->show();
        ui->btn_viewC_layout->setIcon(QIcon());
        ui->btn_viewC_layout->setText("C");
    }
}

void MainWindow::on_lineEdit_cursorPositionX_returnPressed()
{
    int value = ui->lineEdit_cursorPositionX->text().toInt();
    if(value >= 0 && value < m_sysControl->m_imgFile->info().dim[1]) {
        m_sysControl->getImgViewer()->setPositionX(value);
    } else {
        m_sysControl->setPosition();
    }
}

void MainWindow::on_lineEdit_cursorPositionY_returnPressed()
{
    int value = ui->lineEdit_cursorPositionY->text().toInt();
    if(value >= 0 && value < m_sysControl->m_imgFile->info().dim[2]) {
        m_sysControl->getImgViewer()->setPositionY(value);
    } else {
        m_sysControl->setPosition();
    }
}

void MainWindow::on_lineEdit_cursorPositionZ_returnPressed()
{
    int value = ui->lineEdit_cursorPositionZ->text().toInt();
    if(value >= 0 && value < m_sysControl->m_imgFile->info().dim[3]) {
        m_sysControl->getImgViewer()->setPositionZ(value);
    } else {
        m_sysControl->setPosition();
    }
}

void MainWindow::on_btn_view3D_reset_clicked()
{
    m_sysControl->getImgViewer()->reset3D();
}

void MainWindow::on_btn_openFile_clicked()
{
    m_sysControl->openDiskFile();
}

void MainWindow::on_btn_analysis_clicked()
{
    m_sysControl->analysisImg();
}

void MainWindow::on_btn_saveAnalysis_clicked()
{
    m_sysControl->saveAnalysis();
}

void MainWindow::on_action_setting_triggered()
{
    m_sysControl->setSetting();
    loadQss();
}

void MainWindow::on_action_edit_zoom1_triggered()
{
//    m_sysControl->getImgViewer()->zoom(1.0);
}

void MainWindow::on_action_edit_zoom2_triggered()
{
//    m_sysControl->getImgViewer()->zoom(2);
}


void MainWindow::on_action_edit_zoom4_triggered()
{
//    m_sysControl->getImgViewer()->zoom(4);
}

void MainWindow::on_lineEdit_window_returnPressed()
{
    m_sysControl->getImgViewer()->setWindowLevel(
        ui->lineEdit_window->text().toDouble(),
        ui->lineEdit_level->text().toDouble()
    );
}

void MainWindow::on_lineEdit_level_returnPressed()
{
    m_sysControl->getImgViewer()->setWindowLevel(
        ui->lineEdit_window->text().toDouble(),
        ui->lineEdit_level->text().toDouble()
    );
}

void MainWindow::on_btn_view3D_axes_clicked()
{
    static bool flag = true;
    m_sysControl->getImgViewer()->setAxesOpen(flag);
    flag = !flag;
}

void MainWindow::on_horizontalSlider_opacity_valueChanged(int value)
{
    ui->spinBox_opacity->setValue(value);
    m_sysControl->getImgViewer()->setAnlsImgOpacity(value);
}

void MainWindow::on_spinBox_opacity_valueChanged(int arg1)
{
    ui->horizontalSlider_opacity->setValue(arg1);
    m_sysControl->getImgViewer()->setAnlsImgOpacity(arg1);
}

void MainWindow::on_checkBox_anlsImg_stateChanged(int arg1)
{
    if(arg1) {
        m_sysControl->showAnlsImg();
    } else {
        m_sysControl->getImgViewer()->unSetAnalysisImg();
    }
}

void MainWindow::on_btn_viewA_screenshot_clicked()
{
    m_sysControl->saveWindowShot(ui->vtkWidget_viewA->GetRenderWindow());
}

void MainWindow::on_btn_viewS_screenshot_clicked()
{
    m_sysControl->saveWindowShot(ui->vtkWidget_viewS->GetRenderWindow());
}

void MainWindow::on_btn_viewC_screenshot_clicked()
{
    m_sysControl->saveWindowShot(ui->vtkWidget_viewC->GetRenderWindow());
}

void MainWindow::on_btn_view3D_screenshot_clicked()
{
    m_sysControl->saveWindowShot(ui->vtkWidget_view3DSlice->GetRenderWindow());
}

void MainWindow::on_btn_view3D_screenshot_2_clicked()
{
    m_sysControl->saveWindowShot(ui->vtkWidget_view3DInfection->GetRenderWindow());
}

void MainWindow::on_btn_showMessage_clicked()
{
    m_sysControl->showMessage();
}

void MainWindow::on_btn_mode_clicked()
{
    m_sysControl->getImgViewer()->setCrossLineMode(m_sysControl->modeFlag);
    ui->btn_mode->setText(m_sysControl->modeFlag ? "默认模式" : "十字线模式");
    m_sysControl->modeFlag = !m_sysControl->modeFlag;
}

void MainWindow::on_btn_tab_clicked()
{
    if(m_sysControl->tabFlag) {
        m_sysControl->show3DInfection();
        ui->widget_view3D->setCurrentIndex(1);
        ui->btn_tab->setText("查看3D切片");
    } else {
        ui->widget_view3D->setCurrentIndex(0);
        ui->btn_tab->setText("查看3D病灶");
    }
    m_sysControl->tabFlag = !m_sysControl->tabFlag;
}

void MainWindow::on_comboBox_windowLevel_currentIndexChanged(int index)
{
    img_WL wl = img_Info::IMG_WL[index];
    m_sysControl->getImgViewer()->setWindowLevel(wl.window, wl.level);
    ui->lineEdit_window->setText(QString::number(wl.window));
    ui->lineEdit_level->setText(QString::number(wl.level));
}
